#ifndef __HEARTBEAT__
#define __HEARTBEAT__

#include <stdint.h>
#include <stdbool.h>
#include <limits.h>

//#include "protocol.h"

#ifdef __cplusplus
extern "C" {
#endif

struct ChunkServerInfo
{
	char ip[32];
	/* TODO: ip_int != ip[32]: ip[32] may be 0.0.0.0 but ip_int will not be 0 */
	uint32_t ip_int;
	uint16_t port;
	char root[256]; // when chunkserver is running, we chdir to root, so use "./"
	int interval;
	uint64_t capacity;	// in byte
	uint64_t free;		// in byte
	int64_t total_chunk_size; // in byte, may be > capacity - free, due to thin provisioning
	char hostname[HOST_NAME_MAX + 1];
	bool request_server_up;
};

struct ChunkID;

int heartbeat_init(struct ChunkServerInfo* csi);

void heartbeat_add_chunk(uint64_t chunkid);

void heartbeat_remove_chunk(uint64_t chunkid);

void heartbeat_fullupdate();

#define HB_CONTROL_CODE_EXIT			0
#define HB_CONTROL_CODE_UPDATE			1
#define HB_CONTROL_CODE_FULL_UPDATE		2
#define HB_CONTROL_CODE_FILL_REDIS		3
#define HB_CONTROL_CODE_STOP            4

int heartbeat_add_control_code(int code);

int heartbeat_wake_up();

int heartbeat_fini(bool);
void add_ec_journal_size(int64_t size);
void set_big_journal_size(uint64_t size);
void get_self_ip_port(uint32_t *ip, uint16_t *port);

#ifdef __cplusplus
}
#endif

#endif
